/**
 * Form validte settings
 *
 * @requires jquery(Ver: 1.6.1), jquery.validate(1.8.1)
 * @author Huang.zz
 * @version 0.0.1
 */

// Form Validate
function FormValid()
{
    $.validator.setDefaults({submitHandler: function () {
        form.submit();    
    }});
    
    jQuery.validator.addMethod('loginString', function (value, element) {
        return this.optional(element) || /^\w+$/.test(value);    
    }, '');
    
    jQuery.validator.addMethod('availabel_username', function(value, element) {
        return this.optional(element) || alert(element);
    }, '');
}

// Login Form Settings
FormValid.prototype.login = function (form_id) {
    $('#' + form_id).validate({
        rules: {
            username: {
                required: true,
                rangelength: [6, 15],
                loginString: true
            },
            password: {
                required: true,
                rangelength: [8, 24]
            }
        },
        messages: {
            username: {
                required: '用户名不能为空',
                rangelength: '必须满足6-15个字符',
                loginString: '必须是数字, 字母, 下滑线'
            },
            password:{
                required: '密码不为空',
                rangelength: '必须满足8-24个字符'
            }
        },
        //
        focusInvalid: false,
        onkeyup: false,
        //
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        }
    });
}

FormValid.prototype.signup = function (form_id) {
    $('#' + form_id).validate({
        rules: {
            username: {
                required: true,
                rangelength: [6, 15],
                loginString: true
            },
            password: {
                required: true,
                rangelength: [8, 24]
            },
            repeat: {
                required: true,
                equalTo: '#password'
            },
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            username: {
                required: '用户名不能为空',
                rangelength: '必须满足6-15个字符',
                loginString: '必须是数字, 字母, 下滑线'
            },
            password: {
                required: '密码不为空',
                rangelength: '必须满足8-24个字符'
            },
            repeat: {
                required: '请确认密码',
                equalTo: '两次密码必须一致'
            },
            email: {
                required: '请输入电子邮箱地址',
                email: '不符合邮箱规则'
            }
        },
        focusInvalid: false,
        onkeyup: false,
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        }
    });
}
